import 'dart:io';
import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:image_picker/image_picker.dart';
import '../MyButton.dart';
import 'dart:convert';
class ImagePage extends StatefulWidget {
  ImagePage({Key key}) : super(key: key);

  _ImagePageState createState() => _ImagePageState();
}

class _ImagePageState extends State<ImagePage> {
  File _image;
  var _image2;
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("ImagePage"),
      ),
      body: Column(
        children: <Widget>[
          MyButton(text:"拍照",pressed: (){
            _takePhoto();
          }),
          MyButton(text:"选择图片照片",pressed: (){
            _openGalley();
          }),
          _buildImage(this._image),
          _buildImage2(this._image2)
        ],
         
      ),
    );
  }
  _takePhoto() async{
    var image = await ImagePicker.pickImage(source: ImageSource.camera,maxHeight: 400);
    setState(() {
      this._image =  image;
    });
    // this._uploadIamge(image);
  }
  _openGalley() async{
    var image = await ImagePicker.pickImage(source: ImageSource.gallery,maxHeight: 400);
    setState(() {
      this._image =  image;
    });
    // this._uploadIamge(image);
  }
  // 定义一个组件显示图片
  Widget _buildImage(image){
     if(image == null){
       return Text("请选择图片");
     }
     return Image.file(image);
  }
  Widget _buildImage2(image){
     if(image == null){
       return Text("请上传图片");
     }
     return Image.network(image);
  }
  _uploadIamge(_imageDir)async{ 
    FormData formData = new FormData.from({
        'ftID':4, 
        'file': new UploadFileInfo(_imageDir,'xxx.jpg')
    });
    Response result = await Dio().post('https://xcx.qzriji.com/server/public/index/index/saveFile',data: formData);
    setState(() {
      this._image2 = 'https://thd.qzriji.com/Uploads/${result.data['fileUrl']}';
    });
  }
}
